package org.article;

import processing.core.*;

public class SourceArticle extends Article{
	// Indicator if a Section of this document is selected
	private boolean sectionSelected;
	
	public SourceArticle(float f, float h, PApplet parent, PGraphics pgParent, PGraphics pgPreview) {
		super(f, h, parent, pgParent, pgPreview);
		
		int numberOfSections = this.articleSections.size();
		Section tempSection = null;
		
		for(int i=0; i<numberOfSections; i++){
			tempSection = (Section)this.articleSections.get(i);
			
			tempSection.setMovable(true);
		}
	}
	
	
	/**
	 * DRAW Method.
	 * It is mandatory to set the position of the document BEFORE using this method
	 */
	public void draw(){
		super.draw();
		
		drawSectionsFromSource();
	}
	
	/**
	 * Method that draws the sections of this Document.
	 * 		The position is calculated with these rules:
	 * 			posX = posXDocument + padding
	 * 
	 * 			posY0 = posYDocument + padding
	 * 			posYN = posY(N-1) + padding
	 * 		THE PADDING IS ADDED BY THE DRAW METHOD OF THE SECTION!!!!
	 */
	private void drawSectionsFromSource(){
		int numberOfSections = this.articleSections.size();
		Section tempSection;
		
		if(inTransclusion){
			int sX, sY, sW, sH;
			
			sW = articleDrawingPadding * 6;
			sH = articleDrawingPadding * 4;
			
			sX = parent.mouseX - (sW/2);
			sY = parent.mouseY - (sH/2);
			
			sectionSelected = false;	
			
			for(int i=0; i<numberOfSections; i++){
				tempSection = (Section)articleSections.get(i);
				
				if(tempSection.isSelected()){
					sectionSelected = true;
				}
			}
			
			// If a section on this document is selected, a rectangle is drawn
			if(sectionSelected){
				pgDocuments.fill(128);
				pgDocuments.rect(sX, sY, sW, sH);			
			}
			
		}
	}
	
	/**
	 * This method sets the color for all sections in the document
	 * @param newColor
	 */
	public void setSectionsColor(int newColor) {
		int numberOfSections = articleSections.size();
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			tempSection.setSectionColor(newColor);
		}		
	}

	
	/**
	 * Method that checks and 'selects' the selected (pressed) section
	 * @param posX mouse position in x axis
	 * @param posY mouse position in y axis
	 */
	public void selectPressedSection(int posX, int posY){
		int numberOfSections = articleSections.size();
		boolean isOver = false;
		Section tempSection;

		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isOver = tempSection.checkForMouseOver();
			
			if(isOver) {
				tempSection.selectSection(); 
			}
		}
		
		// Disable the functionality of highlighting of sections.
		this.setHighlightStatus(false);
	}
	
	/**
	 * 
	 * @param posX
	 * @param posY
	 * @return 
	 */
	public Section releasePressedSection(int posX, int posY) {
		int numberOfSections = articleSections.size();
		boolean isSelected = false;
		Section tempSection, returnSection;
		
		returnSection = null;

		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isSelected = tempSection.isSelected();
			
			if(isSelected) {
				returnSection = tempSection.releaseSection(); 
			}
		}
		
		this.setHighlightStatus(true);
		
		return returnSection;
	}

	
	public boolean hasSectionSelected(){
		return this.sectionSelected;
	}
		
	/**
	 * Method that enables or disables the highlighting function on this document (SOURCE).
	 * @param status
	 */
	private void setHighlightStatus(boolean status){
		int numberOfSections = articleSections.size();
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			tempSection.setHighlightStatus(status);
		}
	}
	
	public void resetStatus() {
		int numberOfSections = articleSections.size();
		
		Section tempSection;
		
		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			tempSection.setHighlightStatus(true);
			tempSection.forceHighlight(false);
		}
	}


	public Section getPressedSection(int mouseX, int mouseY) {
		int numberOfSections = articleSections.size();
		boolean isSelected = false;
		Section tempSection, returnSection;
		
		returnSection = null;

		for (int i=0; i<numberOfSections; i++){
			tempSection = (Section)articleSections.get(i);
			
			isSelected = tempSection.isSelected();
			
			if(isSelected) {
				returnSection = tempSection.getSection(); 
			}
		}
		
		return returnSection;
	}
}